<template>
  <div class="goods-list">
    <!-- 标签实现跳转页面 -->
    <!-- <router-link class="goods-item"
                 v-for="(item, index) in goodsList"
                 :key="index"
                 :to="'/home/goodsinfo/'+item.id"
                 tag="div">
      <img :src="item.img_url">
      <h3 class="title">{{item.title}}</h3>
      <div class="info">
        <p class="price">
          <span class="new">￥{{item.new_price}}</span>
          <span class="old">￥{{item.old_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>还剩{{item.stock_quantity}}</span>
        </p>
      </div>
    </router-link> -->

    <!-- 编程实现跳转页面 -->
    <div class="goods-item"
         v-for="(item, index) in goodsList"
         :key="index"
         @click="goDetail(item.id)">
      <img :src="item.img_url">
      <h3 class="title">{{item.title}}</h3>
      <div class="info">
        <p class="price">
          <span class="new">￥{{item.new_price}}</span>
          <span class="old">￥{{item.old_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>还剩{{item.stock_quantity}}</span>
        </p>
      </div>
    </div>

    <mt-button type="danger"
               size="large"
               @click="getMore">加载更多</mt-button>
  </div>
</template>

<script>
import { Toast } from 'mint-ui';

export default {
  data() {
    return {
      page: 1,
      goodsList: []
    }
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      this.axios.get(`/getgoods?_page=${this.page}&_limit=4`).then(res => {
        if (res.data.length == 0) {
          Toast('没有更多的商品了');
          return;
        }
        this.goodsList = this.goodsList.concat(res.data);
      })
    },
    getMore() {
      this.page++;
      this.getGoodsList();
    },
    goDetail(id) {
      this.$router.push({ name: 'goodsinfo', params: { id } });
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 8px;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 7px #eee;
    margin: 4px 0;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 293px;
    h3 {
      line-height: 18px;
    }
    img {
      width: 100%;
      height: 180px;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .new {
          color: red;
          font-size: 16px;
          font-weight: 600;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
      }
    }
  }
}
</style>